-- Test that gp_create_restore_point() creates a restore point WAL record
-- on the coordinator and active primary segments.

-- start_matchignore
--
-- # ignore NOTICE outputs from the test plpython function
-- m/NOTICE\:.*pg_xlogdump.*/
--
-- end_matchignore

-- Prepare PITR extension
CREATE EXTENSION IF NOT EXISTS gp_pitr;

-- Create plpython function that will run pg_xlogdump on given WAL segment
-- file and check that the given restore point record exists or not.
CREATE EXTENSION IF NOT EXISTS plpython2u;
CREATE OR REPLACE FUNCTION check_restore_point_record_generated(current_xlogfile_path text, restore_point_name text)
RETURNS bool AS $$
    import os

    cmd = 'pg_xlogdump -r xlog %s 2> /dev/null | grep %s' % (current_xlogfile_path, restore_point_name)
    plpy.notice('Running: %s' % cmd) # useful debug info that is match ignored
    rc = os.system(cmd)

    return (rc == 0)
$$ LANGUAGE plpython2u VOLATILE;

-- Verify that there is currently no restore point record named
-- test_gp_create_restore_point in the coordinator's and active primary
-- segments' current WAL segment file. Running pg_xlogfile_name here() is
-- okay due to the requirement of a fresh gpdemo cluster so timeline ids
-- should all be 1.
SELECT w.gp_segment_id,
       check_restore_point_record_generated(c.datadir || '/pg_xlog/' || w.pg_xlogfile_name, 'test_gp_create_restore_point') AS record_found
FROM gp_segment_configuration c,
     (SELECT -1 AS gp_segment_id, pg_xlogfile_name(pg_current_xlog_location())
     UNION ALL
     SELECT gp_segment_id, pg_xlogfile_name(pg_current_xlog_location())
     FROM gp_dist_random('gp_id')) w
WHERE c.content = w.gp_segment_id AND c.role = 'p'
ORDER BY w.gp_segment_id;

-- Create the restore point records
SELECT true FROM gp_create_restore_point('test_gp_create_restore_point');
CHECKPOINT;

-- Verify that the restore point records have been created on the
-- coordinator and active primary segments.
SELECT w.gp_segment_id,
       check_restore_point_record_generated(c.datadir || '/pg_xlog/' || w.pg_xlogfile_name, 'test_gp_create_restore_point') AS record_found
FROM gp_segment_configuration c,
     (SELECT -1 AS gp_segment_id, pg_xlogfile_name(pg_current_xlog_location())
     UNION ALL
     SELECT gp_segment_id, pg_xlogfile_name(pg_current_xlog_location())
     FROM gp_dist_random('gp_id')) w
WHERE c.content = w.gp_segment_id AND c.role = 'p'
ORDER BY w.gp_segment_id;

-- test simple gp_create_restore_point() error scenarios
SELECT gp_create_restore_point('this_should_fail') FROM gp_dist_random('gp_id');
CREATE TABLE this_ctas_should_fail AS SELECT gp_segment_id AS contentid, restore_lsn FROM gp_create_restore_point('this_should_fail');
